package com.mxx.common.redis;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.*;

/**
 * @Author itzgyw
 * @Date 2020-01-10
 * @Time 18:29
 * @Description:
 */
public class RedisOperationsFactory {

    private RedisConnectionFactory connectionFactory;

    public RedisOperationsFactory(RedisConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    public static RedisOperationsFactory createCustomTemplateFactory(RedisConnectionFactory connectionFactory) {
        return new RedisOperationsFactory(connectionFactory);
    }

    public HashOperations<String, String, String> createHash() {
        RedisTemplate template = createTemplate(connectionFactory);
        return template.opsForHash();
    }

    public <HV, HK> HashOperations<String, HK, HV> createHash(RedisSerializer<HK> hashKeySerializer, RedisSerializer<HV> hashValSerializer) {
        RedisTemplate template = createTemplate(connectionFactory, hashKeySerializer, hashValSerializer);
        return template.opsForHash();
    }

    public <HV> HashOperations<String, String, HV> createHash(RedisSerializer<HV> hashValSerializer) {
        RedisTemplate template = createTemplate(connectionFactory, null, hashValSerializer);
        return template.opsForHash();
    }

    public ListOperations<String, String> createList() {
        RedisTemplate template = createTemplate(connectionFactory);
        return template.opsForList();
    }

    public <V> ListOperations<String, V> createList(RedisSerializer<V> valSerializer) {
        RedisTemplate template = createTemplate(connectionFactory, valSerializer);
        return template.opsForList();
    }

    public SetOperations<String, String> createSet() {
        RedisTemplate template = createTemplate(connectionFactory);
        return template.opsForSet();
    }

    public <V> SetOperations<String, V> createSet(RedisSerializer<V> valSerializer) {
        RedisTemplate template = createTemplate(connectionFactory, valSerializer);
        return template.opsForSet();
    }

    public ValueOperations<String, String> createValue() {
        RedisTemplate template = createTemplate(connectionFactory);
        return template.opsForValue();
    }

    public <V> ValueOperations<String, V> createValue(RedisSerializer<V> valSerializer) {
        RedisTemplate template = createTemplate(connectionFactory, valSerializer);
        return template.opsForValue();
    }


    public static RedisTemplate createTemplate(RedisConnectionFactory factory) {
        return createTemplate(factory, null, null, null);
    }

    public static RedisTemplate createTemplate(RedisConnectionFactory factory, RedisSerializer valSerializer) {
        return createTemplate(factory, valSerializer, null, null);
    }

    public static RedisTemplate createTemplate(RedisConnectionFactory factory, RedisSerializer hashKeySerializer, RedisSerializer hashValSerializer) {
        return createTemplate(factory, null, hashKeySerializer, hashValSerializer);
    }

    public static RedisTemplate createTemplate(RedisConnectionFactory factory, RedisSerializer valSerializer, RedisSerializer hashKeySerializer, RedisSerializer hashValSerializer) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setDefaultSerializer(new StringRedisSerializer());
        template.setConnectionFactory(factory);
        template.setValueSerializer(valSerializer);
        template.setHashValueSerializer(hashValSerializer);
        template.setHashKeySerializer(hashKeySerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

